package com.ubs.training.web.controller;

import com.ubs.training.server.dao.model.Rating;
import com.ubs.training.server.dao.model.RatingAgency;
import com.ubs.training.server.service.CountryService;
import com.ubs.training.server.service.RatingAgencyService;
import com.ubs.training.server.service.RatingService;
import com.ubs.training.web.util.RatingValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.DataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/23/12
 * Time: 3:11 AM
 * To change this template use File | Settings | File Templates.
 */
@Controller
public class RatingController {

    @Autowired
    RatingService svc;

    @Autowired
    CountryService  svCountry;

    @Autowired
    RatingAgencyService svRatingAgency;

    @InitBinder
    void initBind(DataBinder bind) {
        bind.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss"), true));
    }

    @RequestMapping(value = "client/{clientId}/rating/list.do", method = RequestMethod.GET)
    public String getClientRating(@PathVariable("clientId") int clientId, Model model) {
        model.addAttribute("ratings", svc.getAllRatingsByID(clientId));
        //new
        Date date = new Date(System.currentTimeMillis());
        model.addAttribute("date", System.currentTimeMillis());
            return "listrating";
        }

    @RequestMapping(value = "client/{clientId}/rating/{ratingID}/edit.do", method = RequestMethod.GET)
    public String getRating(@PathVariable("ratingID") int ratingID, Model model) {
        Rating rating;
        Date date = new Date(System.currentTimeMillis());
        if (ratingID > 0) {
            rating = svc.getById(ratingID);
            rating.setUpdateTime(date);
        } else {
            rating = new Rating();
            //new

            rating.setUpdateTime(date);
        }
        model.addAttribute("rating", rating);
        model.addAttribute("countries", svCountry.getAllCountries());
        model.addAttribute("ratingagencies", svRatingAgency.getAllRatingAgencies());
        //new
        model.addAttribute("date", System.currentTimeMillis());
        return "editrating1";
    }

    @RequestMapping(value = "client/{clientId}/rating/{ratingID}/edit.do", method = RequestMethod.POST)
    public String saveRating(@PathVariable("clientId") int clientId,
                             @PathVariable("ratingID") int ratingID,
                             Model model,
                             @ModelAttribute("rating") Rating rating, BindingResult res) {
        new RatingValidator(rating, res).validate();
        Date date = new Date(System.currentTimeMillis());
        rating.setRatingID(ratingID);
        rating.setClientID(clientId);
        rating.setUpdateTime(date);
        if (res.hasErrors()){
            model.addAttribute("countries", svCountry.getAllCountries());
            model.addAttribute("ratingagencies", svRatingAgency.getAllRatingAgencies());
            //new
            model.addAttribute("date", System.currentTimeMillis());
            return "editrating1";
        }
        svc.saveRating(rating);
        return "redirect:/client/{clientId}/rating/list.do";
    }

    @RequestMapping(value = "client/{clientId}/rating/{ratingID}/delete.do", method = RequestMethod.GET)
    public String deleteRating(@PathVariable("ratingID") int ratingID, Model model) {
        svc.deleteRating(svc.getById(ratingID));
        return "redirect:/client/{clientId}/rating/list.do";
    }

}



